package com.example.guideapp.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.example.guideapp.R;
import com.example.guideapp.activity.AddMyFestivalActivity;
import com.example.guideapp.activity.ShowMyFestivalActivity;
import com.example.guideapp.adapter.MyDayListViewAdapter;
import com.example.guideapp.bean.MyFestival;
import com.example.guideapp.utils.DatabaseManage;
import com.example.guideapp.utils.MySqliteDatabaseHelper;

import java.util.List;

/**
 * Created by Administrator on 2017/10/16.
 */

public class Fragment3 extends Fragment {

    public static final String LISTVIEW_ITEM = "listview_item";


    private Button btn_add_myfestival;
    private ListView listView;

    private LayoutInflater mInflater;

    private MySqliteDatabaseHelper helper;

    private List<MyFestival> list;
    private MyDayListViewAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment3,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        helper = DatabaseManage.getInstance(getActivity());
        mInflater = LayoutInflater.from(getActivity());

        if (view == null){
            view = mInflater.inflate(R.layout.fragment3,null);
        }

        btn_add_myfestival = (Button) view.findViewById(R.id.btn_add_myfestival);
        listView = (ListView) view.findViewById(R.id.fragmen3_listView);

        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from MyFestival",null);
        list = DatabaseManage.myFestivalCursorToList(cursor);
        db.close();

        adapter = new MyDayListViewAdapter(list,getActivity());

        listView.setAdapter(adapter);


        btn_add_myfestival.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d("fragment3", "onClick: ");
                Intent i = new Intent(getActivity(), AddMyFestivalActivity.class);
                startActivity(i);
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), ShowMyFestivalActivity.class);
                intent.putExtra(LISTVIEW_ITEM,list.get(i));
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from MyFestival",null);
        list = DatabaseManage.myFestivalCursorToList(cursor);
        adapter.refresh(list);
    }
}
